package com.huajin.iot.system.controller;

import com.huajin.iot.common.annotation.Log;
import com.huajin.iot.common.config.Constant;
import com.huajin.iot.common.controller.BaseController;
import com.huajin.iot.common.domain.Tree;
import com.huajin.iot.common.service.DictService;
import com.huajin.iot.common.utils.*;
import com.huajin.iot.system.domain.DeptDO;
import com.huajin.iot.system.domain.RoleDO;
import com.huajin.iot.system.domain.UserDO;
import com.huajin.iot.system.service.RoleService;
import com.huajin.iot.system.service.UserService;
import com.huajin.iot.system.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"用户管理接口"})
@RequestMapping("/sys/user")
@RestController
public class UserController extends BaseController {
    private String prefix = "system/user";
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    DictService dictService;


    @ApiOperation("所有用户列表")
    @PostMapping("/list")
//    @RequiresPermissions("sys:user:user")
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<UserDO> sysUserList = userService.list(query);
        int total = userService.count(query);
        PageUtils pageUtil = new PageUtils(sysUserList, total);
        return pageUtil;
    }

    @ApiOperation("新增用户")
    @RequiresPermissions("sys:user:add")
    @Log("新增用户")
    @PostMapping("/save")
    @ResponseBody
    R save(UserDO user) {
        user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
        if (userService.save(user) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("更新用户")
    @RequiresPermissions("sys:user:edit")
    @Log("更新用户")
    @PostMapping("/update")
    @ResponseBody
    R update(UserDO user) {
        if (userService.update(user) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("删除单一用户")
    @RequiresPermissions("sys:user:remove")
    @Log("删除单一用户")
    @PostMapping("/remove")
    @ResponseBody
    R remove(Long id) {
        if (userService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("批量删除用户")
    @RequiresPermissions("sys:user:batchRemove")
    @Log("批量删除用户")
    @PostMapping("/batchRemove")
    @ResponseBody
    R batchRemove(@RequestParam("ids[]") Long[] userIds) {
        int r = userService.batchremove(userIds);
        if (r > 0) {
            return R.ok();
        }
        return R.error();
    }


    @ApiOperation("用户第一次登录修改默认密码")
    @Log("用户第一次登录修改默认密码")
    @PostMapping("/resetPwd")
    @ResponseBody
    R resetPwd(UserVO userVO) {
        try {
            userService.resetPwd(userVO, getUser());
            return R.ok();
        } catch (Exception e) {
            return R.error(1, e.getMessage());
        }

    }

    @ApiOperation("admin重置用户密码")
    @RequiresPermissions("sys:user:resetPwd")
    @Log("admin重置用户密码")
    @PostMapping("/adminResetPwd")
    @ResponseBody
    R adminResetPwd(UserVO userVO) {
        try {
            userService.adminResetPwd(userVO);
            return R.ok();
        } catch (Exception e) {
            return R.error(1, e.getMessage());
        }

    }

    @GetMapping("/tree")
    @ResponseBody
    public Tree<DeptDO> tree() {
        Tree<DeptDO> tree = new Tree<DeptDO>();
        tree = userService.getTree();
        return tree;
    }

    @ApiOperation("查看个人信息")
    @GetMapping("/personal")
    R personal() {
        UserDO userDO = userService.get(getUserId());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("user", userDO);
        resultMap.put("hobbyList", dictService.getHobbyList(userDO));
        resultMap.put("sexList", dictService.getSexList());
        return R.ok(resultMap);
    }

    @ApiOperation("上传用户图像")
    @ResponseBody
    @PostMapping("/uploadImg")
    R uploadImg(@RequestParam("avatar_file") MultipartFile file, String avatar_data, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = userService.updatePersonalImg(file, avatar_data, getUserId());
        } catch (Exception e) {
            return R.error("更新图像失败！");
        }
        if (result != null && result.size() > 0) {
            return R.ok(result);
        } else {
            return R.error("更新图像失败！");
        }
    }
}
